package de.backtomedieval.activities.gamemenu;

import java.util.ArrayList;
import de.backtomedieval.scenarios.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import de.backtomedieval.R;
import de.backtomedieval.activities.startscreen.NewProfile;
import de.backtomedieval.activities.startscreen.ProfileStartscreen;
import de.backtomedieval.activities.startscreen.Startscreen;
import de.backtomedieval.game.Helper;
import de.backtomedieval.game.Player;
import de.backtomedieval.main.Game;

public class ScenarioMenu extends Activity {
	
	private AbstractScenario scenario = null;
	private ArrayList<AbstractScenario> m_scenarios;
	private ScenarioAdapter m_scenarioadapter;
	private ListView lv1;
	private GridView grid_main;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );

		setContentView(R.layout.scenariomenu); 

		lv1=(ListView)findViewById(R.id.ListViewScenario);
		m_scenarios = new ArrayList<AbstractScenario>();
		this.m_scenarioadapter = new ScenarioAdapter(this, R.layout.row, m_scenarios);
		lv1.setAdapter(this.m_scenarioadapter);
		ArrayList<AbstractScenario> u = AbstractScenario.getScenarioList();
		for (int i=0; i<u.size(); i++) {
			m_scenarioadapter.add(u.get(i));
		}
		m_scenarioadapter.notifyDataSetChanged();

		lv1.setTextFilterEnabled(true);

		lv1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onScenarioListChoose(position);
			}
		});


		grid_main = (GridView)findViewById(R.id.GridViewMenu);
		grid_main.setAdapter(new ImageAdapter(this));
        grid_main.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	onScenarioInfoMenuChoose(position);
            }
        });



	}

	protected void onScenarioInfoMenuChoose(int position) {
		if (position==0) {
			onButtonPlay();
		}
	}

	@Override
	public void onResume(){
	   super.onResume();
	   m_scenarioadapter.notifyDataSetChanged();
	   Game.setLastActivity(this);
	}


	protected void onButtonPlay() {		
		Game g = new Game();
		Player player = new Player(Game.getPlayername());
		player.addCity(Game.getCityname());
		g.addPlayer(player);

		Game.setStartScenario(scenario);
		g.setScenario(Game.getStartScenario());
		g.getScenario().atGamestart();
		Game.startGame();
		
		Intent newgameIntent = new Intent(ScenarioMenu.this, Gamemenu.class);
		startActivity(newgameIntent);
		this.finish();
	}



	protected void onScenarioListChoose(int position) {
		if (position!=-1) {
			scenario = m_scenarioadapter.getItem(position);
			TextView tv_scenarioinfo = (TextView) findViewById(R.id.scenariomenu_description);
			
			String desc = "<b>" + scenario.getName() + "</b><br><br>"+ scenario.getDescription();
			
			tv_scenarioinfo.setText(Html.fromHtml(desc, imgGetter, null));
			LinearLayout l1 =(LinearLayout)findViewById(R.id.LayoutScenarioChoose);
			l1.setVisibility(8);
			LinearLayout l2 =(LinearLayout)findViewById(R.id.LayoutScenarioInfo);
			l2.setVisibility(0);
		}
	}

	@Override
	public void onBackPressed() {
		LinearLayout l1 =(LinearLayout)findViewById(R.id.LayoutScenarioInfo);
		int vis = l1.getVisibility();
		if (vis == 0) {
			switchToScenarioList();
		} else {
			Intent changeToIntent = new Intent(this,ProfileStartscreen.class);
			startActivity(changeToIntent);
			this.finish();
		}

		return;
	}

	private void switchToScenarioList() {
		LinearLayout l1 =(LinearLayout)findViewById(R.id.LayoutScenarioChoose);
		l1.setVisibility(0);
		LinearLayout l2 =(LinearLayout)findViewById(R.id.LayoutScenarioInfo);
		l2.setVisibility(8);
	}

	private class ScenarioAdapter extends ArrayAdapter<AbstractScenario> {

		private ArrayList<AbstractScenario> items;

		public ScenarioAdapter(Context context, int textViewResourceId, ArrayList<AbstractScenario> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			AbstractScenario o = items.get(position);
			if (o != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				ImageView icon=(ImageView)v.findViewById(R.id.icon);
				icon.setImageResource(R.drawable.icon);
				if (tt != null) {
					tt.setText(o.getName());
				}
				if(bt != null){
					bt.setText(o.getStateString());
					

				}
			}
			return v;
		}
	}

	public class ImageAdapter extends BaseAdapter{
		Context mContext;
		public static final int ACTIVITY_CREATE = 10;
		public ImageAdapter(Context c){
			mContext = c;
		}

		public int getCount() {
			return 1;
		}


		public View getView(int position, View convertView, ViewGroup parent) {
			View v;
			if(convertView==null){
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.gridiconlarge, null);
				TextView tv = (TextView)v.findViewById(R.id.icon_text);
				ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
				tv.setText(R.string.play);
				iv.setImageResource(R.drawable.icon);

			}
			else{
				v = convertView;
			}
			return v;
		}
		
		
		
		public Object getItem(int position) {
			return null;
		}
		
		public long getItemId(int position) {
			return 0;
		}
	}
	
	
	   ImageGetter imgGetter = new Html.ImageGetter() {
	        @Override
	        public Drawable getDrawable(String source) {
	              Resources res = ScenarioMenu.this.getResources();
	              Drawable drawable = res.getDrawable(Helper.getImageFromString(source));
	              drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/3, drawable.getIntrinsicHeight()/3);
	              return drawable;
	        }
	    };
}
